function F = solve_eqm_cf2_inner(param, const, integ, eqm)
%% Counterfactual: Export Promotion w/ Balanced Trade  

    % use from baseline equilibrium
    P_sF        = eqm.P_sF;
    mbar        = eqm.mbar;
    X_init_eqm  = eqm.X;   

    % t
    t = const.t;
    sigma_s = param.sigma_s;

    % read in parameters
    kappa   = param.kappa;
    alpha_m = param.alpha_m; 
    alpha_s = param.alpha_s;
    beta_v  = param.beta_v; 
    beta_m  = param.beta_m;    
    sigma   = param.sigma; 
    f_vH    = param.f_vH;
    f_vF    = param.f_vF;
    mu_E    = param.mu_E;
    sigma_E = param.sigma_E;   
    w       = param.w;
    Q       = param.Q;
    Nf      = param.Nf;
    
    % read in constants    
    gamma   = const.gamma;
    k0      = const.k0;
    C_m     = const.C_m;
    C_v0    = const.C_v0;
    C_x0    = const.C_x0;   
    phi_v   = const.phi_v;
    phi_y   = const.phi_y;
    phi_s   = const.phi_s;
    D_F     = const.D_F;
    lnzQ    = const.lnzQ;
        
    % read in all the integral from previous round
    E_zm0 = integ.E_zm0;
    E_zm1 = integ.E_zm1;
    E_zv0 = integ.E_zv0;
    E_zv1 = integ.E_zv1;
    E_zx0 = integ.E_zx0;
    E_zx1 = integ.E_zx1;
        
    % adjust for level in case integrals too large
    rescale = max(1,10^floor(log10(max(E_zx1))-1));
    E_zx0 = E_zx0/rescale;
    E_zx1 = E_zx1/rescale;
    E_zv0 = E_zv0/(rescale^(1/beta_v));
    E_zv1 = E_zv1/(rescale^(1/beta_v));
    E_zm0 = E_zm0/(rescale^(1/beta_m));        
    E_zm1 = E_zm1/(rescale^(1/beta_m));      

    
%% Solve the equilibrium    

    % Setting
    tol = 1e-12;
    maxIter = 1000;

    % Initial guess 
    D_Hnew = eqm.D_H;
    c_new = eqm.c;    
    e_new = 1;    
    P_s = 1; 
    
    % Iteration
    err = 1;
    iter = 0;
    while err >= tol && iter < maxIter

        % new guess
        D_H = D_Hnew;
        c = c_new;
        e = e_new;

        % D(q,E)
        rv_ads = ((1-t)*D_H/f_vH).^(1/(beta_v-1))./(((1-t)*D_H/f_vH).^(1/(beta_v-1))+(e^sigma*D_F/f_vF).^(1/(beta_v-1)));
        D0 = D_H;
        D1 = rv_ads.*D_H + (1-rv_ads)*e^sigma.*D_F;        
        
        % useful terms
        f_v1 = rv_ads.^beta_v*f_vH + (1-rv_ads).^(beta_v)*f_vF;
        C_v1 = (1./(sigma*f_v1.*w)).^(1/beta_v); 

        % Pi(q)
        Pi0 = D0.^gamma.*(c.^alpha_m*P_s^alpha_s).^((1-sigma)*gamma).*C_x0;
        power = beta_v/(beta_v-1);
        DE = (D_H.^power+(1-t)^(-1/(beta_v-1))*(e^sigma*D_F).^power).^(1/power);
        Pi1 = Pi0.*(DE./D_H).^gamma;      
        
        % V(q) and M(q)
        Vbar = C_v0.*Pi0.^(1/beta_v).*E_zv0+rv_ads.*C_v1.*Pi1.^(1/beta_v).*E_zv1;
        V = sum(phi_v.*repmat(Vbar,Q,1),2)';
        M = C_m.*(Pi0.^(1/beta_m).*E_zm0+Pi1.^(1/beta_m).*E_zm1);

        % theta_v(q) and theta_m(q)
        xi = M./V;
        theta_v = 1-exp(-kappa*xi);
        theta_m = (1-exp(-kappa*xi))./xi;
        theta_m(isnan(theta_m))=kappa;       
        theta_m(theta_m==0)=kappa;
        
        % X(q,E)
        X0 = Pi0.*E_zx0;
        X1 = Pi1.*E_zx1;
        X_m = alpha_m*(sigma-1)/sigma*(X0+X1);

        % P(q)^(1-sigma)
        Psig = (X0./D0+rv_ads.*X1./D1);
        %check = Psig.^(1/(1-sigma))./eqm.Psig.^(1/(1-sigma))
        
        % D_m(q)
        TempD = theta_v./M.*c.^(sigma-1).*X_m;
        TempD(isnan(TempD))=0;
        D_m = sum(phi_y.*phi_v.*repmat((TempD)',1,Q),1);

        % total exports
        rv_rev = rv_ads.*D_H./D1;        
        B1 = sum((1-rv_rev).*X1);

        % total cost of subsidy
        T = t/(sigma*beta_v*(1-t))*B1;

        % total spending on services
        X = sum(X0 + X1); 
        L = 1/sigma*((1-alpha_m-alpha_s)*(sigma-1)+1/beta_v+alpha_m/beta_m);
        %X_s = 1/sigma*((1-alpha_m-alpha_s)*(sigma-1)+1/beta_v+alpha_m/beta_m)*(L-T)/L*X_init_eqm + 1/(sigma*gamma)*X + (alpha_s*(sigma-1)/sigma)*X;
        X_s = (1-alpha_m*(sigma-1)/sigma)*(L-T)/L*X_init_eqm;   

        % P_s (new guess)
        P_sH = (mbar/sum(V)*sum(phi_s.*Psig))^(1/(1-sigma));
        P_s = ( P_sH^(1-sigma_s)+(e*P_sF)^(1-sigma_s) )^(1/(1-sigma_s));
        
        % imports (for checking)    
        B2 = (e*P_sF/P_s)^(1-sigma_s)*X_s;
        %B1-B2

        % D_s(q)
        X_sH = (P_sH/P_s)^(1-sigma_s)*X_s;
        D_s = phi_s.*X_sH/sum(phi_s.*Psig);
    
        % D(q) (new guess)
        D_Hnew = D_m+D_s;

        % c(q) (new guess)
        c_new = (theta_m./V.*sum(phi_y.*phi_v.*repmat(Psig,Q,1),2)').^(1/(1-sigma));           
        
        % e (new guess)
        e_new = P_sH/P_sF*(B1/X_sH)^(1/(1-sigma_s));

        % update
        D_Hnew = D_H + 0.2*(D_Hnew - D_H);
        c_new = c + 0.2*(c_new - c);
        e_new = e + 0.2*(e_new - e);
        iter = iter+1;      
        
%             % check 
%             disp('goods market clearing--demand')        
%             [X_s + sum(X_m)]
%             disp('goods market clearing--supply')        
%             [sum(X0)+ sum(rv_rev.*X1)]

        % check convergence
        err1 = max(abs(D_Hnew-D_H)./D_H);
        err2 = max(abs(c_new-c)./c);
        err = err1 + err2;

    end
    
    % Warning
    if iter == maxIter
        msg = 'User Warning: Maximum Iterations Reached.';
        warning(msg)
    end 

    % Adjust back
    Pi0 = Pi0/rescale;
    Pi1 = Pi1/rescale; 
    
    % Save the Export Decision
    ExportCutoffQ = (k0*lnzQ-log(sigma*gamma)+log(kron(ones(Nf,1),Pi1)-kron(ones(Nf,1),Pi0))-mu_E)/sigma_E;
    ExportProbQ = normcdf(ExportCutoffQ);        


    %% welfare change
    
    eqm_cf.welfare_chg = (L-T)/L/(P_s/1)-1;

    eqm_cf.household_income = 1/sigma*((1-alpha_m-alpha_s)*(sigma-1)+1/beta_v+alpha_m/beta_m + 1/gamma)*eqm.X*(L-T)/L;


%% Return

    eqm_cf.Pi0 = Pi0;
    eqm_cf.Pi1 = Pi1;
    eqm_cf.D_H = D_H;
    eqm_cf.D_F = D_F;
    eqm_cf.D0 = D0;
    eqm_cf.D1 = D1;
    eqm_cf.D_s = D_s;
    eqm_cf.D_m = D_m;
    eqm_cf.c = c;
    eqm_cf.Psig = Psig;
    eqm_cf.P = Psig.^(1/(1-sigma));
    eqm_cf.X_m = X_m;
    eqm_cf.X0 = X0;
    eqm_cf.X1 = X1;
    eqm_cf.V = V;
    eqm_cf.Vbar = Vbar;
    eqm_cf.M = M;
    eqm_cf.xi = xi;
    eqm_cf.theta_v = theta_v;
    eqm_cf.theta_m = theta_m;  
    eqm_cf.rv_ads = rv_ads;
    eqm_cf.rv_rev = rv_ads.*D_H./D1; 
    eqm_cf.ExportCutoffQ = ExportCutoffQ;
    eqm_cf.ExportProbQ = ExportProbQ;
    eqm_cf.C_v1 = C_v1;

    eqm_cf.mbar = mbar; 
    eqm_cf.P_sF = P_sF;
    eqm_cf.P_sH = P_sH;
    eqm_cf.P_s = P_s;
    eqm_cf.B1 = B1;
    eqm_cf.B2 = B2;
    eqm_cf.X_sH = X_sH;
    eqm_cf.X_s = X_sH + B2;
    eqm_cf.X = X;    
    eqm_cf.e = e; 
    eqm_cf.T = T;
    eqm_cf.L = L;

    eqm_cf.iter_inner = iter;
    eqm_cf.err_inner = err;
    
    F = eqm_cf;

    
end